﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Benefit.Models.Sys;
namespace Benefit.Models.Month
{

    public class AccountMonthTradeProductAnalysis
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 交易月
        /// </summary>
        public int Month { get; set; }
        /// <summary>
        /// 交易品种
        /// </summary>
        public int ProductId { get; set; }
        /// <summary>
        /// 交易品种
        /// </summary>
        public virtual Product Product { get; set; }
        /// <summary>
        /// 多空方向
        /// </summary>
        public int Direction { get; set; }

        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }

        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }





        ///// <summary>
        ///// 删除当月所有记录
        ///// </summary>
        ///// <param name="tradeHistoryId"></param>
        //public void Delete(int month, int year, DB.DBManager db)
        //{
        //    var query = from t in db.AccountMonthTradeProductAnalysis where t.Month == month && t.Year == year select t;
        //    foreach (Data.Month.AccountMonthTradeProductAnalysis c in query)
        //    {
        //        db.AccountMonthTradeProductAnalysis.Remove(c);
        //    }
        //    db.SaveChanges();
        //}



        ///// <summary>
        ///// 保存账号月变化情况
        ///// </summary>
        ///// <param name="tradeHistoryId">插入时间编号</param>
        ///// <param name="pdate">插入时间</param>
        //public void InitMonthAccountProductAnalysis(int month, int year, DB.DBManager db)
        //{
        //    var query = db.Accounts.ToList();
        //    foreach (Sys.Account account in query)
        //    {
        //        #region i为0是多，i为1是空
        //        for (int i = 0; i < 2; i++)
        //        {
        //            var _query = (from t in db.AccountDayTradeProductAnalysis where t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year && t.AccountId == account.Id && t.Direction == i select t).ToList();
        //            foreach (Day.AccountDayTradeProductAnalysis instru in _query)
        //            {
        //                var query2 = (from t in db.AccountMonthTradeProductAnalysis where t.Month == instru.TradeHistory.MPdate && t.Year == instru.TradeHistory.YPdate && t.AccountId == account.Id && t.Direction == i && t.ProductId == instru.ProductId select t).ToList();
        //                var query3 = (from t in db.AccountDayTradeProductAnalysis where t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year && t.AccountId == account.Id && t.Direction == i && t.ProductId == instru.ProductId select t).ToList();
        //                AccountMonthTradeProductAnalysis change = new AccountMonthTradeProductAnalysis();
        //                change.AccountId = account.Id;
        //                change.Direction = i;
        //                change.ProductId = instru.ProductId;
        //                if (query2.Count() > 0)//有记录更新
        //                {
        //                    change = query2.First();
        //                }
        //                if (_query.Count() > 0)
        //                {
        //                    change.AvgPostionTime = query3.Sum(a => a.AvgPostionTime);
        //                    change.MaxReturn = query3.Sum(a => a.MaxReturn);
        //                    change.OverStopLoss = query3.Sum(a => a.OverStopLoss);
        //                    change.PingJinPing = query3.Sum(a => a.PingJinPing);
        //                }
        //                change.Year = year;
        //                change.Month = month;
        //                if (query2.Count() == 0)
        //                {
        //                    db.AccountMonthTradeProductAnalysis.Add(change);
        //                }
        //            }

        //        }
        //        #endregion
        //    }
        //    db.SaveChanges();
        //}
    }
}